//
//  AppOrderListController.m
//  gungunchou
//
//  Created by lyzbmacos3 on 2018/12/4.
//  Copyright © 2018年 xskj. All rights reserved.
//

#import "AppOrderListController.h"
#import "OrderListCell.h"
#import "XHSegmentControl.h"
#import "OrderListModel.h"

@interface AppOrderListController () <UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *table;

@property (nonatomic, assign) NSInteger pageIndex;

@property (strong, nonatomic) OrderListModel *orderListModel;
@end

@implementation AppOrderListController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.pageIndex = 1;
    [self.table registerNib:[UINib nibWithNibName:@"OrderListCell" bundle:nil] forCellReuseIdentifier:@"OrderListCell"];
    
    self.table.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.pageIndex ++;
        [self fetchList];
    }];
    [self fetchList];
    
//    self.view.frame = CGRectMake(0, CGRectGetMinY(self.view.frame)-64, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame));
}

- (void)fetchList
{
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:self.orderStatus,@"OrderStatus", @(self.pageIndex),@"PageIndex", @"10",@"PageSize", TestUsrId,@"UserID", nil];
    [MBProgressHUD showHUDAddedTo:KWindow animated:YES];
    [[CGNetManage shared] httpGetOrderListWithParams:params Completed:^(NSDictionary *data) {
        [MBProgressHUD hideHUDForView:KWindow animated:YES];
        [self endrefresh];
        if (data && data.count > 0) {
            self.orderListModel = [OrderListModel mj_objectWithKeyValues:data];
            [self.table reloadData];
//            for (NSDictionary *proDic in data) {
//                ProductModel *model = [ProductModel mj_objectWithKeyValues:proDic];
//                [self.dataArr addObject:model];
                
//            }
            
//            [self.collect reloadData];
        }
        else {
            self.pageIndex -= 1;
        }
        
    } fail:^(NSError *err) {
        [MBProgressHUD hideHUDForView:KWindow animated:YES];
        [self endrefresh];
        self.pageIndex -= 1;
    }];
}

- (void)endrefresh
{
    [self.table.mj_header endRefreshing];
    [self.table.mj_footer endRefreshing];
}


#pragma mark - table delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.orderListModel.LOrderList.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 150;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    OrderListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OrderListCell"];
    OrderInfoModel *model = self.orderListModel.LOrderList[indexPath.row];
    if (model.LOrderInfo && model.LOrderInfo.count>0) {
        cell.model = model;
    }
    
    return cell;
}

@end
